<template>
  <div class="login-container">
    <div class="login-content">
      <h1 class="title">校园智慧竞赛平台</h1>
      <h2 class="subtitle">管理员登录</h2>

      <div class="login-form">
        <div class="form-item">
          <label>用户名：</label>
          <input
            v-model="loginForm.username"
            type="text"
            class="form-input"
            placeholder="请输入用户名"
          />
        </div>

        <div class="form-item">
          <label>密码：</label>
          <input
            v-model="loginForm.password"
            type="password"
            class="form-input"
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
          />
        </div>

        <div class="button-group">
          <button class="login-btn" @click="handleLogin" :disabled="isLoading">
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
        </div>
      </div>

    </div>

    <!-- 自定义弹窗 -->
    <div v-if="showModal" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>{{ modalTitle }}</h3>
          <button class="close-btn" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
          <p>{{ modalText }}</p>
          <p v-if="modalContactInfo" class="contact-info">{{ modalContactInfo }}</p>
        </div>
        <div class="modal-footer">
          <button class="confirm-btn" @click="closeModal">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { adminLoginService, getAdminInfoService } from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js'
import { useAdminStore } from '@/stores/admin.js'

const router = useRouter()
const tokenStore = useTokenStore()
const adminStore = useAdminStore()

// 表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 加载状态
const isLoading = ref(false)

// 弹窗控制
const showModal = ref(false)
const modalTitle = ref('')
const modalText = ref('')
const modalContactInfo = ref('')

// 显示弹窗
const showCustomModal = (title, text, contactInfo = '') => {
  modalTitle.value = title
  modalText.value = text
  modalContactInfo.value = contactInfo
  showModal.value = true
}

// 登录处理
const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    showCustomModal('提示', '请输入用户名和密码')
    return
  }

  isLoading.value = true

  try {
    // 调用登录接口
    const result = await adminLoginService(loginForm)

    if (result.code !== 200) {
      showCustomModal('登录失败', result.message || '登录失败，请检查用户名和密码')
      return
    }

    // 存储token
    tokenStore.setToken(result.data.token)

    // 获取管理员详细信息
    try {
      const adminInfoResult = await getAdminInfoService()
      if (adminInfoResult.code === 200) {
        adminStore.setAdminInfo(adminInfoResult.data)
      }
    } catch (error) {
      console.warn('获取管理员信息失败:', error)
    }

    // 登录成功，跳转到主页
    showCustomModal('登录成功', '欢迎进入校园智慧竞赛平台管理端')
    setTimeout(() => {
      router.push('/dashboard')
    }, 1500)

  } catch (error) {
    console.error('登录失败:', error)
    showCustomModal('登录失败', error.message || '登录失败，请检查网络连接')
  } finally {
    isLoading.value = false
  }
}

// 关闭弹窗
const closeModal = () => {
  showModal.value = false
}
</script>

<style scoped>
@import '@/assets/css/login.css';
</style>